package com.template.ejb.rest.bean;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.apache.commons.beanutils.BeanUtils;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;

import com.template.ejb.domain.Person;
import com.template.ejb.rest.UserResource;
import com.template.ejb.rest.dto.PersonDTO;
import com.template.ejb.rest.dto.PersonListDTO;
import com.template.ejb.service.UserService;

/**
 * 
 * 
 */
@Stateless
public class UserResourceBean implements UserResource {

	@EJB
	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	private void checkPerson(Person person) {
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();

		kbuilder.add(ResourceFactory.newClassPathResource(
				"licenseApplication.drl", getClass()), ResourceType.DRL);

		if (kbuilder.hasErrors()) {
			System.err.println(kbuilder.getErrors().toString());
		}

		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		StatelessKnowledgeSession ksession = kbase
				.newStatelessKnowledgeSession();

		ksession.execute(person);

		System.out.println("Result: " + person.getIsDeleted());

	}

	@Override
	public PersonDTO create(PersonDTO personDTO) throws IllegalAccessException,
			InvocationTargetException {

		Person person = new Person();
		BeanUtils.copyProperties(person, personDTO);
		person.setId(null);

		/**
		 * 
		 */
		this.checkPerson(person);

		userService.create(person);

		BeanUtils.copyProperties(personDTO, person);

		return personDTO;

	}

	@Override
	public Person findById(Long id) {
		return userService.findById(id);
	}

	@Override
	public Person update(PersonDTO personDTO) throws IllegalAccessException,
			InvocationTargetException {
		Person person = new Person();
		BeanUtils.copyProperties(person, personDTO);
		return userService.update(person);
	}

	@Override
	public Person findByLogin(String login) {
		return userService.findByLogin(login);
	}

	@Override
	public PersonListDTO getPersons(int offset, int limit)
			throws IllegalAccessException, InvocationTargetException {

		PersonListDTO personListDTO = new PersonListDTO();
		List<Person> persons = userService.getPersons(offset, limit);

		for (Person person : persons) {
			PersonDTO personDTO = new PersonDTO();
			BeanUtils.copyProperties(personDTO, person);
			personListDTO.add(personDTO);
		}

		return personListDTO;
	}

	// private static final String QUEUE_CONNECTION_FACTORY =
	// "ConnectionFactory";
	// private static final String LONG_PROCESS_QUEUE = "queue/myQueue";
	//	
	// private JbpmEngine jbpmEngine = JbpmEngine.getInstance();
	//	
	// @Override
	// public void message(MathDTO value) {
	// JMSProducer jmsProducer = JMSProducer.getInstance();
	// jmsProducer.sendMessage(value,
	// UserResourceBean.QUEUE_CONNECTION_FACTORY,
	// UserResourceBean.LONG_PROCESS_QUEUE);
	// }
	//
	// @Override
	// public void execute(String value) {
	// jbpmEngine.deployFromClasspath("bpm/processdefinition.jpdl.xml");
	// jbpmEngine.startByKey("processdefinition");
	// }
	//
	// @Override
	// public void rule() {
	//
	// KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
	//
	// KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
	// .newKnowledgeBuilder();
	//
	// kbuilder.add(ResourceFactory.newClassPathResource(
	// "licenseApplication.drl", getClass()), ResourceType.DRL);
	//
	// if (kbuilder.hasErrors()) {
	// System.err.println(kbuilder.getErrors().toString());
	// }
	//
	// kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
	// StatelessKnowledgeSession ksession = kbase
	// .newStatelessKnowledgeSession();
	// Applicant applicant = new Applicant("Mr John Smith", 19);
	// ksession.execute(applicant);
	//
	// System.out.println("******** IsValid: " + applicant.isValid());
	//
	// }

}
